package com.qiusuo0226.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiusuo0226.common.constant.NumConstant;
import com.qiusuo0226.common.util.ConvertTool;
import com.qiusuo0226.core.mapper.UserInfoMapper;
import com.qiusuo0226.core.pojo.dto.UserInfoDTO;
import com.qiusuo0226.core.pojo.entity.UserInfoDO;
import com.qiusuo0226.core.pojo.query.UserInfoAddBean;
import com.qiusuo0226.core.service.UserInfoService;
import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.security.config.SecurityProperty;
import com.qiusuo0226.security.util.AesTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户信息 逻辑处理层
 * @author qiusuo
 * @date 2023/6/24 21:13
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoDO> implements UserInfoService {

    @Autowired
    private SecurityProperty property;

    @Override
    public JsonResultModel add(UserInfoAddBean bean) {
        try {
            // AES cbc模式加密账号
            byte[] accountBytes = AesTool.encryptCBC(bean.getAccount(),
                    ConvertTool.hexToBytes(property.getAesKey()),
                    ConvertTool.hexToBytes(property.getAesIv()));
            String account = ConvertTool.base64EncodeToStr(accountBytes);
            // 查询账号是否已存在
            int count = baseMapper.countByAccount(account);
            // 账号未存在
            if (count == NumConstant.N0) {
                UserInfoDO userInfo = new UserInfoDO();
                userInfo.setUserAccount(account);
                byte[] passBytes = AesTool.encryptCBC(bean.getPwd(),
                        ConvertTool.hexToBytes(property.getAesKey()),
                        ConvertTool.hexToBytes(property.getAesIv()));
                String password = ConvertTool.base64EncodeToStr(passBytes);
                userInfo.setUserPass(password);
                int insert = baseMapper.insert(userInfo);
                if (insert > NumConstant.N0) {
                    return JsonResultModel.success("新增用户成功");
                } else {
                    return JsonResultModel.failure("30006", "新增用户持久化失败");
                }
            } else {
                return JsonResultModel.failure("30001", "账号已存在");
            }
        } catch (Exception e) {
            log.error("", e);
            return JsonResultModel.failure("30005", "用户新增异常：" + e.toString() + ":" + e.getMessage());
        }
    }


    @Override
    public UserInfoDTO queryByAccountAndPass(String account, String password) {
        try {
            // AES cbc模式加密账号、密码
            byte[] accountBytes = AesTool.encryptCBC(account,
                    ConvertTool.hexToBytes(property.getAesKey()),
                    ConvertTool.hexToBytes(property.getAesIv()));
            account = ConvertTool.base64EncodeToStr(accountBytes);
            byte[] passBytes = AesTool.encryptCBC(password,
                    ConvertTool.hexToBytes(property.getAesKey()),
                    ConvertTool.hexToBytes(property.getAesIv()));
            password = ConvertTool.base64EncodeToStr(passBytes);
            return baseMapper.selectByAccountAndPass(account, password);
        } catch (Exception e) {
            log.error("根据账号密码查询用户信息异常", e);
        }
        return null;
    }
}
